package com.liang.lessonIp;

import java.net.InetAddress;
import java.net.UnknownHostException;

//测试ip
public class TestnetAddress {
    public static void main(String[] args) {
        try {
            //查询本机地址
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            InetAddress localhost = InetAddress.getByName("localhost");
            InetAddress host = InetAddress.getLocalHost();
            System.out.println(localhost);
            System.out.println(host);
            System.out.println(inetAddress);
            //查询网站ip地址
            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2);

            //常用方法
            System.out.println(inetAddress2.getAddress());
            System.out.println(inetAddress2.getHostAddress());//ip
            System.out.println(inetAddress2.getCanonicalHostName());//规范的名字
            System.out.println(inetAddress2.getHostName());//域名，或者自己电脑的名字
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }
}
